package com.example.discover.view;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.example.common.gamedetail.GameDetailActivity;
import com.example.common.network.IStartGameDetailActivity;
import com.example.discover.DiscoverAdapter;
import com.example.discover.R;
import com.example.discover.databinding.DiscoverFragmentBinding;
import com.example.discover.viewmodel.DiscoverViewModel;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.PriorityQueue;

@Route(path = "/business/discover")
public class DiscoverFragment extends Fragment {

    private DiscoverViewModel mViewModel;
    private DiscoverFragmentBinding binding;
    DiscoverAdapter discoverAdapter;

    public static DiscoverFragment newInstance() {
        return new DiscoverFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.discover_fragment, container, false);
        if (mViewModel == null) {
            mViewModel = new ViewModelProvider(requireActivity()).get(DiscoverViewModel.class);
        }
        //recyclerView的路由创建
        discoverAdapter = new DiscoverAdapter(mViewModel);
        binding.discoverRecyclerview.setItemAnimator(new DefaultItemAnimator());
        binding.setDiscoverViewModel(mViewModel);
        binding.setLifecycleOwner(getViewLifecycleOwner());
        View view = binding.getRoot();
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //设置路由和布局，缺一不可

        mViewModel.getIsDone().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                binding.discoverRecyclerview.setLayoutManager(new LinearLayoutManager(requireActivity()));
                binding.discoverRecyclerview.setAdapter(discoverAdapter);
                Log.i("dfghjiuy", "onChanged: ");
            }
        });

        //adapter的item绑定点击事件
        discoverAdapter.iStartGameDetailActivity = new IStartGameDetailActivity() {
            @Override
            public void openById(Integer id) {
                Intent intent = new Intent(requireActivity(), GameDetailActivity.class);
                intent.putExtra("ID",id);
                startActivity(intent);
            }
        };


    }

    /**
     * 图片加载类
     */
    private class LocalImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }

}